home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / web / literateprog / web.tex (.txt) < prev    next >
LaTeX Document  |  1992-09-23  |  73KB  |  1,340 lines

  1. % Page layout
  2. \input webmac
  3. \output{\setbox0=\box255}\eject % get rid of spurious WEBMAC page
  4. \font\man=manfnt scaled \magstep3
  5. \font\CompJtitle=ambx10 scaled\magstep4
  6. \font\CompJabstract=amb10
  7. \font\tenssb=amssmc10
  8. \font\tenss=amss10
  9. \font\tenssi=amssi10
  10. \font\eightss=helvetica at 8truebp
  11. \font\eightssi=helveticai at 8truebp
  12. \font\eightssb=helveticab at 8truebp
  13. \font\eighttt=amtt8
  14. \font\ninerm=amr9
  15. \let\mc=\ninerm % medium caps for names like PASCAL
  16. \newdimen\pagewidth \newdimen\pageheight \newdimen\ruleht
  17. \hsize=177mm  \vsize=249mm
  18. \parindent=1em % this is needed for WEB output
  19. \pagewidth=\hsize \pageheight=\vsize \ruleht=1pt
  20. \abovedisplayskip=11pt plus 3pt minus 8pt
  21. \abovedisplayshortskip=0pt plus 3pt
  22. \belowdisplayskip=11pt plus 3pt minus 8pt
  23. \belowdisplayshortskip=6pt plus 3pt minus 3pt
  24. \newif\iftitle
  25. \def\titlepage{\global\titletrue} % for pages without headlines
  26. \def\leftheadline{\hbox to \pagewidth{%
  27.     \vbox to 8pt{}\hss \eightrm D. E. KNUTH\hss}}
  28. \def\rightheadline{\hbox to \pagewidth{%
  29.     \vbox to 8pt{}\hss \eightrm LITERATE PROGRAMMING\hss}}
  30. \hoffset=-.25in \voffset=-.6in
  31. \newinsert\lefttop \newinsert\righttop
  32. \count\lefttop=1000 \count\righttop=1000
  33. \dimen\lefttop=\maxdimen \dimen\righttop=\maxdimen
  34. \skip\lefttop=25pt plus 3pt minus 3pt
  35. \skip\righttop=\skip\lefttop
  36. \def\leftfloat{\insert\lefttop\bgroup
  37.   \floatingpenalty=0
  38.   \penalty0
  39.   \vbox\bgroup}
  40. \def\rightfloat{\insert\righttop\bgroup
  41.   \floatingpenalty=0
  42.   \penalty0
  43.   \vbox\bgroup}
  44. \def\endfloat{\egroup\egroup}
  45. \def\onepageout#1{\shipout\vbox{ % here we define one page of output
  46.     \offinterlineskip % butt the boxes together
  47.     \vbox to 9mm{ % this part goes on top of the regular pages
  48.       \iftitle % the next is used for title pages
  49.         \global\titlefalse % reset the titlepage switch
  50.         \hbox to\pagewidth{\leaders\CJrule\hfill}
  51.       \else\ifodd\pageno \rightheadline\else\leftheadline\fi\fi
  52.       \vfill} % this completes the \vbox to 9mm
  53.     \vbox to \pageheight{
  54.       #1 % now insert the main information
  55.       \boxmaxdepth=\maxdepth
  56.       } % this completes the \vbox to \pageheight
  57.     \baselineskip=7mm \lineskiplimit=0pt
  58.     \hbox to\pagewidth{%
  59.       \ifodd\pageno\hfil\tenss submitted to THE COMPUTER JOURNAL%
  60.         \tenssb\quad\folio
  61.       \else\tenssb\folio\quad
  62.         \tenss submitted to THE COMPUTER JOURNAL\hfil\fi}
  63.     }
  64.   \advancepageno}
  65. \output{\onepageout{\unvbox255}}
  66. \newbox\partialpage
  67. \def\begindoublecolumns{\begingroup
  68.   \output={\global\setbox\partialpage=\vbox{\unvbox255}}\eject
  69.   \output={\doublecolumnout} \hsize=84mm \vsize=510mm}
  70. \def\enddoublecolumns{\output={\balancecolumns}\eject
  71.   \endgroup \pagegoal=\vsize}
  72. \def\doublecolumnout{\dimen0=\pageheight
  73.   \advance\dimen0 by-\ht\partialpage \splittopskip=\topskip
  74.   \ifdim\ht\lefttop>0pt \setbox255=\vbox{\unvbox\lefttop
  75.     \setbox0=\lastbox\unvbox0\vskip\skip\lefttop\unvbox255}\fi
  76.   \setbox0=\vsplit255 to\dimen0
  77.   \ifdim\ht\righttop>0pt \setbox255=\vbox{\unvbox\righttop
  78.     \setbox0=\lastbox\unvbox0\vskip\skip\righttop\unvbox255}\fi
  79.   \setbox2=\vsplit255 to\dimen0
  80.   \onepageout\pagesofar
  81.   \unvbox255 \penalty\outputpenalty}
  82. \def\pagesofar{\unvbox\partialpage
  83.   \wd0=\hsize \wd2=\hsize \hbox to\pagewidth{\box0\hfil\box2}}
  84. \def\balancecolumns{\setbox0=\vbox{\unvbox255} \dimen0=\ht0
  85.   \advance\dimen0 by\topskip \advance\dimen0 by-\baselineskip
  86.   \divide\dimen0 by2 \splittopskip=\topskip
  87.   {\vbadness=10000 \loop \global\setbox3=\copy0
  88.     \global\setbox1=\vsplit3 to\dimen0
  89.     \ifdim\ht3>\dimen0 \global\advance\dimen0 by1pt \repeat}
  90.   \setbox0=\vbox to\dimen0{\unvbox1}
  91.   \setbox2=\vbox to\dimen0{\unvbox3}
  92.   \pagesofar}
  93. \def\CJrule{\hrule height\ruleht}
  94. \baselineskip=11pt
  95. \parskip=0pt plus 1pt
  96. \def\beginsection #1\par{\goodbreak\vskip9mm plus4mm minus 2mm
  97.   \vbox{\CJrule width \hsize \kern5pt}
  98.   \kern-3pt
  99.   \nointerlineskip
  100.   \leftline{\strut\bf#1}
  101.   \CJrule
  102.   \kern12pt\nobreak\noindent\ignorespaces}
  103. \def\caption #1. #2.{\leftline{\def\TeX{T\kern-.2em\lower.5ex\hbox{E}X}%
  104.     \tenssb Figure #1.\enspace\tenss#2.}}
  105. \def\WEB{{\tt WEB}}
  106. \def\PASCAL{{\mc PASCAL}}
  107. \def\sec{{\tensy x}}
  108. \def\<{$\langle\,$}
  109. \def\>{$\,\rangle$}
  110. \newbox\circlebox
  111. \setbox\circlebox=\hbox{\man Y}
  112. \def\encircle#1{\kern6pt\hbox to\wd\circlebox{\hss\tt#1\hss}\kern-\wd\circlebox
  113.   \raise10pt\copy\circlebox\kern6pt}
  114. \def\ttverbatim{\begingroup \tt \parindent=0pt \obeylines
  115.   \uncatcodespecials \catcode`/=0 \obeyspaces}
  116. \let\endverbatim=\endgroup
  117. {\obeyspaces\global\let =\ } % let active space = control space
  118. \def\uncatcodespecials{\def\do##1{\catcode`##1=12 }\dospecials}
  119. \def\cvdots{\kern3pt\qquad\smash\vdots}
  120. \newcount\refno \newif\ifshowit
  121. \def\ref{\showittrue\makeref}
  122. \def\silentref{\showitfalse\makeref}
  123. \def\references{} % this will grow until it holds all the references
  124. \def\makeref#1#2{\advance\refno by1 \edef#1{{\the\refno}}%
  125.   \toks0=\expandafter{\references}%
  126.   {\def\rm{\eightss}\def\sl{\eightssi}\def\bf{\eightssb}\def\tt{\eighttt}%
  127.     \def\TeX{T\kern-.2em\lower.5ex\hbox{E}\kern-.000em X}%
  128.     \xdef\references{\the\toks0 \noexpand\item{\the\refno.}#2\par}}%
  129.   \ifshowit\edef\next{\spacefactor=\the\spacefactor\space}%
  130.    $^{\the\refno}$\next\fi}
  131. \hyphenation{Dijk-stra}
  132. \hyphenchar\tentt=-1 % no hyphenation in the typewriter font
  133. \titlepage
  134. \leftline{\kern13mm\CompJtitle Literate Programming}
  135. \kern6mm
  136. \CJrule
  137. \kern4.5mm
  138. \leftline{\kern13mm\bf Donald E. Knuth}
  139. \kern2pt
  140. \leftline{\kern13mm\eightrm Computer Science Department, Stanford University,
  141.   Stanford, CA 94305, USA}
  142. \kern4mm
  143. \CJrule
  144. \kern6mm
  145. \leftline{\kern13mm\vbox{\hsize=151mm\CompJabstract\noindent
  146. The author and his associates have been experimenting for the past several
  147. years with a programming language and documentation system called \WEB.
  148. This paper presents \WEB\ by example, and discusses why the new
  149. system appears to be an improvement over previous ones.}}
  150. \bigskip\bigskip
  151. \begindoublecolumns
  152. \beginsection A. INTRODUCTION
  153. The past ten years have witnessed substantial improvements in programming
  154. methodology. This advance, carried out under the banner of ``structured
  155. programming,'' has led to programs that are more reliable and easier to
  156. comprehend; yet the results are not entirely satisfactory. My purpose
  157. in the present paper is to propose another motto that may be appropriate
  158. for the next decade, as we attempt to make further progress in the
  159. state of the art. I believe that the time is ripe for significantly
  160. better documentation of programs, and that we can best achieve this by
  161. considering programs to be {\it works of literature}. Hence, my title:
  162. ``Literate Programming.''
  163. Let us change our traditional attitude to the construction of programs:
  164. Instead of imagining that our main task is to instruct a {\it computer\/}
  165. what to do, let us concentrate rather on explaining to {\it human beings\/}
  166. what we want a computer to do.
  167. The practitioner of literate programming can be regarded as an essayist, whose
  168. main concern is with exposition and excellence of style. Such an author,
  169. with thesaurus in hand, chooses the names of variables carefully and explains
  170. what each variable means. He or she strives for a program that is
  171. comprehensible because its concepts have been introduced in an order that
  172. is best for human understanding, using a mixture of formal and informal
  173. methods that re\"\i nforce each other.
  174. I dare to suggest that such advances in documentation are possible because
  175. of the experiences I've had during the past several years while working
  176. intensively on software development. By making use of several ideas that
  177. have existed for a long time, and by applying them systematically in a
  178. slightly new way, I've stumbled across a method of composing programs
  179. that excites me very much. In fact, my enthusiasm is so great that I must warn
  180. the reader to discount much of what I shall say as the ravings of a fanatic
  181. who thinks he has just seen a great light.
  182. Programming is a very personal activity, so I can't be certain that what has
  183. worked for me will work for everybody. Yet the impact of this new approach on
  184. my own style has been profound, and my excitement has continued unabated
  185. for more than two years. I~enjoy the new methodology so much that it is hard
  186. for me to refrain from going back to every program that I've ever written
  187. and recasting it in ``literate'' form. I~find myself unable to resist working
  188. on programming tasks that I would ordinarily have assigned to student
  189. research assistants; and why? Because it seems to me that at last I'm able
  190. to write programs as they should be written. My programs are not only
  191. explained better than ever before; they also are better programs,
  192. because the new methodology encourages me to do a better job. For these
  193. reasons I am compelled to write this paper, in hopes that my experiences
  194. will prove to be relevant to others.
  195. I must confess that there may also be a bit of malice in my choice of
  196. a title. During the 1970s I was coerced like everybody else into adopting
  197. the ideas of structured programming, because I couldn't bear to be found
  198. guilty of writing {\it unstructured\/} programs. Now I have a chance
  199. to get even. By coining the phrase ``literate programming,'' I am imposing
  200. a moral commitment on everyone who hears the term; surely nobody wants
  201. to admit writing an {\it il{}literate\/} program.
  202. \beginsection B. THE \WEB\ SYSTEM
  203. I hope, however, to demonstrate in this paper that the title is not merely
  204. wordplay. The ideas of literate programming have been embodied in a language
  205. and a suite of computer programs that have been developed at Stanford
  206. University during the past few years as part of my research on algorithms
  207. and on digital typography. This language and its associated programs
  208. have come to be known as the \WEB\ system. My goal in what follows is
  209. to describe the philosophy that underlies \WEB, to present examples
  210. of programs in the \WEB\ language, and to discuss what may be the future
  211. implications of this work.
  212. I chose the name \WEB\ partly because it was one of the few three-letter
  213. words of English that hadn't already been applied to computers. But as time
  214. went on, I've become extremely pleased with the name, because I~think
  215. that a complex piece of software is, indeed, best regarded as a {\it web\/}
  216. that has been delicately pieced together from simple materials. We
  217. understand a complicated system by understanding its simple parts, and by
  218. understanding the simple relations between those parts and their immediate
  219. neighbors. If we express a program as a web of ideas, we can emphasize
  220. its structural properties in a natural and satisfying way.
  221. \WEB\ itself is chiefly a combination of two other languages:
  222. (1)~a document formatting language and (2)~a programming language.
  223. My prototype \WEB\ system uses \TeX\ as the document formatting
  224. language and \PASCAL\ as the programming language, but the same
  225. principles would apply equally well if other languages were
  226. substituted. Instead of \TeX, one could use a language like Scribe
  227. or Troff; instead of \PASCAL, one could use {\mc ADA}, {\mc ALGOL},
  228. {\mc LISP}, {\mc COBOL}, {\mc FORTRAN}, {\mc APL}, {\mc C}, etc., or
  229. even assembly language. The main point is that \WEB\ is inherently
  230. bilingual, and that such a combination of languages proves to be much
  231. more powerful than either single language by itself. \WEB\ does not
  232. make the other languages obsolete; on the contrary, it enhances them.
  233. I naturally chose \TeX\ to be the document formatting language, in the
  234. first \WEB\ system, because \TeX\ is my own creation;\ref\TeXbook{D. E. Knuth,
  235. {\sl The \TeX book}. Addison-Wesley, Reading, Mass., U.S.A. (1983).}
  236. I wanted to acquire a lot of experience in harnessing \TeX\ to a variety
  237. of different tasks.  I~chose \PASCAL\ as the programming language because
  238. it has received such widespread support from educational institutions all
  239. over the world; it is not my favorite language for system programming, but
  240. it has become a ``second language'' for so many programmers that it
  241. provides an exceptionally effective medium of communication. Furthermore
  242. \WEB\ itself has a macro-processing ability that makes \PASCAL's
  243. limitations largely irrelevant.
  244. Document formatting languages are newcomers to the computing scene, but
  245. their use is spreading rapidly. Therefore I'm confident that we will be
  246. able to expect each member of the next generation of programmers to be
  247. familiar with a document language as well as a programming language,
  248. as part of their basic education. Once a person knows both of the
  249. underlying languages, there's no trick at all to learning \WEB, because
  250. the \WEB\ user's manual is fewer than ten pages long.
  251. A \WEB\ user writes a program that serves
  252. as the source language for two different system routines. (See Figure~1.)
  253. One line of processing is called {\it weaving\/} the web; it produces
  254. a document that describes the program clearly and that facilitates program
  255. maintenance. The other line of processing is called {\it tangling\/} the
  256. web; it produces a machine-executable program. The program and its
  257. documentation are both generated from the same source, so they are
  258. consistent with each other.
  259. \bigskip
  260. \centerline{\vbox{
  261.     \halign{&\hss#\hss\cr
  262.     &&&\TeX\cr
  263.     \noalign{\vskip-4pt}
  264.     &&\encircle{TEX}&\enspace\rightarrowfill\enspace&\encircle{DVI}\cr
  265.     \multispan2\hfil\smash{\raise4pt\hbox{\tt WEAVE}\kern-1pt}$\nearrow$ \cr
  266.     \noalign{\vskip6pt}
  267.     \encircle{WEB}\cr
  268.     \noalign{\vskip6pt}
  269.     \multispan2\hfil\smash{\lower6pt\hbox{\tt TANGLE}\kern-1pt}$\searrow$ \cr
  270.     &&\encircle{PAS}&\enspace\rightarrowfill\enspace&\encircle{REL}\cr
  271.     \noalign{\vskip-2pt}
  272.     &&&\mc\ PASCAL\ \cr}
  273.     }}
  274. \nobreak\medskip
  275. \caption 1. Dual usage of a {\tt WEB} file.
  276. \bigbreak
  277. Let's look at this process in slightly more detail. Suppose you have
  278. written a \WEB\ program and put it into a computer text file called
  279. {\tt COB.WEB} (say). To generate hardcopy documentation for your program,
  280. you can run the {\tt WEAVE} processor; this is a system program that takes
  281. the file {\tt COB.WEB} as input and produces another file {\tt COB.TEX}
  282. as output. Then you run the \TeX\ processor, which takes {\tt COB.TEX}
  283. as input and produces {\tt COB.DVI} as output. The latter file,
  284. {\tt COB.DVI}, is a ``device-independent'' binary description of how
  285. to typeset the documentation, so you can get printed output by applying
  286. one more system routine to this file.
  287. You can also follow the other branch of Figure~1, by running the
  288. {\tt TANGLE} processor; this is a system program that takes the file
  289. {\tt COB.WEB} as input and produces a new file {\tt COB.PAS} as output.
  290. Then you run the \PASCAL\ compiler, which converts {\tt COB.PAS} to
  291. a binary file {\tt COB.REL} (say). Finally, you can run your program
  292. by loading and executing {\tt COB.REL}. The process of ``compile, load,
  293. and go'' has been slightly lengthened to ``tangle, compile, load, and go.''
  294. \beginsection C. A COMPLETE EXAMPLE
  295. Now it's time for me to stop presenting general platitudes and to move on
  296. to something tangible. Let us look at a real program that has been written
  297. in \WEB. The numbered paragraphs that follow are the actual output of a
  298. \WEB\ file that has been ``woven'' into a document; a computer has also
  299. generated the indexes that appear at the program's end. If my claims for
  300. the advantages of literate programming have any merit, you should be able
  301. to understand the following description more easily than you could
  302. have understood the same program when presented in a more conventional
  303. way. However, I am trying here to explain the format of \WEB\ documentation
  304. at the same time as I am discussing the details of a nontrivial algorithm,
  305. so the description below is slightly longer than it would be if it were
  306. written for people who already have been introduced to \WEB.
  307. \silentref\Dijk{O.-J.~Dahl, E.~W. Dijkstra, and C.~A.~R. Hoare,
  308. {\sl Structured Programming}. Academic Press, London and New York (1972).}
  309. \silentref\goto{D. E. Knuth, Structured programming with {\bf go to}
  310. statements. {\sl Computing Surveys\/ \bf6}, 261--301 (1974).}
  311. Here, then, is the computer-generated output:
  312. \bigskip
  313. \CJrule
  314. \medskip
  315. \begingroup
  316. \def\prune\input webmac{\input primes.contents}
  317. \def\Z#1#2#3{\line{\ignorespaces#1\ \dotfill\ {\tensy x}#2}}
  318. \def\M#1.{\MN#1.\iftrue\medbreak\startsection\ignorespaces}
  319. \def\firstmod{1}
  320. \def\N#1.#2.{\MN#1.\iftrue\nobreak
  321.   \ifx\modno\firstmod\medskip\else\bigskip\fi
  322.   \CJrule\medbreak\startsection
  323.   {\bf\ignorespaces#2.\quad}\ignorespaces}
  324. \def\inx{\par\medbreak
  325.   \def\:##1, {\par\hangindent2em\noindent##1:\kern1em}
  326.   \def\[##1]{$\underline{##1}$}
  327.   \rm \rightskip0pt plus2.5em \tolerance10000 \let\*=\lapstar
  328.   \hyphenpenalty10000 \parindent0pt}
  329. \def\fin{\par\bigskip\CJrule\medbreak
  330.   \parfillskip0pt plus1fil
  331.   \def\note##1##2.{\hfil\penalty-1\hfilneg\quad{\eightrm##1 ##2.}}
  332.   \def\U{\note{Used in}}
  333.   \def\:{\par\hangindent 2em}\let\*=*}
  334. \let\con=\par
  335. \parskip=0pt
  336. \expandafter\prune\input primes
  337. \endgroup
  338. \beginsection D. HOW THE EXAMPLE WAS SPECIFIED
  339. Everything reproduced above, from the table of contents preceding the
  340. program to the indexes of identifiers and section names at the end,
  341. was generated by applying the program {\tt WEAVE} to a source file
  342. {\tt PRIMES.WEB} written in the \WEB\ language. Let us now look at that
  343. file {\tt PRIMES.WEB}, in order to get an idea of what a \WEB\ user
  344. actually types.
  345. There's no need to show very much of {\tt PRIMES.WEB}, however, because
  346. that file is reflected quite faithfully by the formatted output. Figure~2
  347. contains enough of the \WEB\ source to indicate the general flavor;
  348. a reader who is familiar with the rudiments of \TeX\ will be able to
  349. reconstruct all of {\tt PRIMES.WEB} by looking only at the formatted
  350. output and Figure~2.
  351. \leftfloat
  352. \ttverbatim
  353. /hrule
  354. /medskip
  355. \font\ninerm=cmr9
  356. \let\mc=\ninerm % medium caps
  357. \def\WEB{{\tt WEB}}
  358. \def\PASCAL{{\mc PASCAL}}
  359. \def\[{\ifhmode\ \fi$[\mkern-2mu[$}
  360. \def\]{$]\mkern-2mu]$\ }
  361. /cvdots
  362. \hyphenation{Dijk-stra}
  363. /medskip
  364. @* Printing primes: An example of \WEB.
  365. The following program is essentially the same
  366. as Edsger Dijkstra's @^Dijkstra, Edsger@>
  367. ``first example of step-wise program
  368. composition,'' found on pages 26--39
  369. of his {\sl Notes on Structured
  370. Programming},$^\Dijk$  but it has been
  371. translated into the \WEB\ language. @.WEB@>
  372. /medskip
  373. \[Double brackets will be used in what
  374. follows to enclose comments relating to \WEB\
  375. /cvdots
  376. an informal top-level description.\]
  377. /medskip
  378. @p @<Program to print the first thousand
  379. prime numbers@>
  380. /endverbatim
  381. \medskip
  382. \caption 2a. The beginning of {\tt PRIMES.WEB}.
  383. \medskip
  384. \hrule
  385. \endfloat
  386. Figure 2a starts with \TeX\ commands (not shown in full) that make it
  387. convenient to typeset double brackets $[\mkern-2mu[\ldots]\mkern-2mu]$
  388. and to give special typographic treatment to names like `\WEB' and `\PASCAL'.
  389. A \WEB\ user generally begins by declaring such special aspects of the
  390. document format; for example, if nonstandard fonts of type are needed,
  391. they are usually stated first. It may also be necessary to specify the
  392. correct hyphenation of non-English words that appear in the document.
  393. Then comes `{\tt@*}', which starts the program proper. \WEB\ uses the
  394. symbol `{\tt@}' as an escape character for special instructions to the
  395. {\tt WEAVE} and {\tt TANGLE} processors. Everything between such special
  396. commands is either expressed in \TeX\ language or in \PASCAL\ language,
  397. depending on the context.
  398. Each section of the program begins either with `{\tt@ }' (i.e., at-sign
  399. and space) or `{\tt@*}' (i.e., at-sign and asterisk); \WEB\ supplies the
  400. section numbers automatically. The latter case, `{\tt@*}', denotes a
  401. {\it major section\/} of the program, for which a special title is given.
  402. This title will appear in boldface type, and it will also appear in the
  403. table of contents, and as a running headline on all pages of the
  404. woven documentation until another major section begins. Each major section
  405. starts at the top of a page. (Such page beginnings have been indicated
  406. by horizontal lines in our example, because \WEB's normal output format
  407. has been adapted to the format of this journal. The output of {\tt WEAVE}
  408. usually has a lot more white space, and the individual lines of text
  409. are usually quite a bit wider.)
  410. The lines that follow in Figure~2a show a few more \WEB\ instructions:
  411. `{\tt@\char`^}' marks the beginning of an index entry to be set in roman
  412. type; `{\tt@>}' marks the end of an argument to a \WEB\ command;
  413. `{\tt@.}'\ marks the beginning of an index entry to be set in typewriter
  414. type; `{\tt@p}' marks the beginning of the \PASCAL\ program; and `{\tt@<}'
  415. marks the beginning of a top-level description, i.e., of a section
  416. name in the \WEB\ program.
  417. \rightfloat
  418. \ttverbatim
  419. /hrule
  420. /medskip
  421. @ This program has no input, because we want
  422. to keep it rather simple.  The result of the
  423. program will be to produce a list of the
  424. first thousand prime numbers, and this list
  425. will appear on the |output| file.
  426. /medskip
  427. Since there is no input, we declare the value
  428. |m=1000| as a compile-time constant. The
  429. program itself is capable of generating the
  430. first |m| prime numbers for any positive |m|,
  431. as long as the computer's finite limitations
  432. are not exceeded.
  433. /medskip
  434. \[The program text below specifies the
  435. ``expanded meaning'' of `\X2:Program to print
  436. $\ldots$ numbers\X'; notice that it involves
  437. the top-level descriptions of three other
  438. sections. When those top-level descriptions
  439. are replaced by their expanded meanings, a
  440. syntactically correct \PASCAL\ program will
  441. be obtained.\]
  442. /medskip
  443. @<Program to print...@>=
  444. program print_primes(output);
  445. const @!m=1000;
  446. @<Other constants of the program@>@;
  447. var @<Variables of the program@>@;
  448. begin @<Print the first |m| prime numbers@>;
  449. /endverbatim
  450. \medskip
  451. \caption 2b. The \WEB\ code that generated \sec2.
  452. \ttverbatim
  453. /bigskip
  454. /hrule
  455. /medskip
  456. @ In order to keep this program reasonably
  457. free of notations that are uniquely
  458. \PASCAL esque, \[and in order to illustrate
  459. /cvdots
  460. The first three macro definitions here are
  461. parametric; the other two are simple.\]
  462. /medskip
  463. @d print_string(#)==write(#)
  464.  {put a given string into the |output| file}
  465. @d print_integer(#)==write(#:1)
  466.  {put a given integer into the |output|
  467.   file, in decimal notation, using only as
  468.   many digit positions as necessary}
  469. @d print_entry(#)==write(#:ww)
  470.  {like |print_integer|, but
  471.   |ww| character positions are filled,
  472.   inserting blanks at the left}
  473. @d new_line==write_ln
  474.  {advance to a new line in the |output| file}
  475. @d new_page==page
  476.  {advance to a new page in the |output| file}
  477. /endverbatim
  478. \medskip
  479. \caption 2c. The \WEB\ code that generated \sec6.
  480. \medskip
  481. \hrule
  482. \endfloat
  483. Figure 2b immediately follows Figure~2a in the \WEB\ file. This material
  484. is what generated \sec2 of the documentation, and it illustrates the
  485. bilingual nature of \WEB: The commentary at the beginning of each section
  486. is typed in \TeX\ language, and the program text at the end is typed
  487. in \PASCAL\ language.
  488. Language-switching between \TeX\ and \PASCAL\ is occasionally desirable.
  489. For example, when you refer to technical details about the program, you
  490. usually want to describe them in \PASCAL, hence you want {\tt WEAVE} to format
  491. them with the typographic conventions it uses for \PASCAL\ programs.
  492. Conversely, when you put comments in a \PASCAL\ program, you want
  493. the text of those comments to be formatted by \TeX\ in the normal way.
  494. \WEB\ files use vertical bars to introduce \PASCAL\ formatting in the
  495. midst of \TeX\ formatting; for example, Figure~2b says `{\tt the
  496. |output| file}' in order to typeset `the \\{output} file'.
  497. The program text in Figure~2b begins with `{\tt@<}' instead of with the
  498. `{\tt@p}' command used in Figure~2a, because the program text in~\sec2
  499. is the expansion of a specific top-level description. Notice that the
  500. top-level description has been abbreviated to `{\tt@<Program to print...@>}'.
  501. Since the names of sections tend to be rather long, it is a nuisance to
  502. type them in full each time; \WEB\ allows you to type `{\tt...}'\ after you
  503. have given enough text to identify the remainder uniquely.
  504. The `{\tt@!}'\ operation in the program text of Figure~2b governs the
  505. underlining of index entries. The `{\tt@;}'\ specifies an invisible symbol
  506. that has the effect of a semicolon in \PASCAL\ syntax. Commands such as these
  507. are comparatively unimportant, but they are available for polishing up
  508. the final documentation when you want to maintain fine control.
  509. Figure 2c shows key portions of the \WEB\ text that generated \sec6.
  510. Notice that the command `{\tt@d}' introduces a macro definition.
  511. All features of \WEB\ that appear in our example program are illustrated
  512. in Figures 2a, 2b, and~2c; the remainder of {\tt PRIMES.WEB} simply
  513. uses the same conventions again and again. In fact, most of the \WEB\
  514. file is much simpler than the examples shown here; Figure~2 has
  515. illustrated only the difficult parts.
  516. \beginsection E. THE TANGLED OUTPUT
  517. Figure 3 shows the \PASCAL\ program {\tt PRIMES.PAS} that results when
  518. {\tt TANGLE} is applied to {\tt PRIMES.WEB}. This program is not intended
  519. for human consumption---it's only supposed to be readable by a \PASCAL\
  520. compiler---so {\tt TANGLE} does not go to great pains to produce a
  521. beautiful format. Notice that underlines have been removed from the
  522. identifier names, and that all of the letters have been converted to
  523. uppercase (except in strings); {\tt TANGLE} tries to produce a format
  524. that will be acceptable to a standard \PASCAL\ compiler.
  525. {\tt TANGLE} removes all of the commentary in the \WEB\ file, but it
  526. inserts new comments of its own. If for some reason you need
  527. to correlate the tangled \PASCAL\ code with the woven documentation,
  528. you can find the program text for, say, \sec8 by looking between
  529. the comments `{\tt\char`\{8:\char`\}}' and `{\tt\char`\{:8\char`\}}'.
  530. A comparison of Figure~3 to Figure~2 should make it clear why the
  531. {\tt TANGLE} processor has acquired its name.
  532. \rightfloat
  533. \ttverbatim
  534. /hrule
  535. /medskip
  536. {1:}{2:}PROGRAM PRINTPRIMES(OUTPUT);
  537. CONST M=1000;{5:}RR=50;CC=4;WW=10;{:5}{19:}
  538. ORDMAX=30;{:19}VAR{4:}
  539. P:ARRAY[1..M]OF INTEGER;{:4}{7:}
  540. PAGENUMBER:INTEGER;PAGEOFFSET:INTEGER;
  541. ROWOFFSET:INTEGER;C:0..CC;{:7}{12:}J:INTEGER;
  542. K:0..M;{:12}{15:}JPRIME:BOOLEAN;{:15}{17:}
  543. ORD:2..ORDMAX;SQUARE:INTEGER;{:17}{23:}
  544. N:2..ORDMAX;{:23}{24:}
  545. MULT:ARRAY[2..ORDMAX]OF INTEGER;{:24}
  546. BEGIN{3:}{11:}{16:}J:=1;K:=1;P[1]:=2;{:16}
  547. {18:}ORD:=2;SQUARE:=9;{:18};
  548. WHILE K<M DO BEGIN{14:}REPEAT J:=J+2;{20:}
  549. IF J=SQUARE THEN BEGIN ORD:=ORD+1;{21:}
  550. SQUARE:=P[ORD]*P[ORD];{:21}{25:}
  551. MULT[ORD-1]:=J;{:25};END{:20};{22:}N:=2;
  552. JPRIME:=TRUE;
  553. WHILE(N<ORD)AND JPRIME DO BEGIN{26:}
  554. WHILE MULT[N]<J DO MULT[N]:=MULT[N]+P[N]+P[N]
  555. ;IF MULT[N]=J THEN JPRIME:=FALSE{:26};N:=N+1;
  556. END{:22};UNTIL JPRIME{:14};K:=K+1;P[K]:=J;
  557. END{:11};{8:}BEGIN PAGENUMBER:=1;
  558. PAGEOFFSET:=1;
  559. WHILE PAGEOFFSET<=M DO BEGIN{9:}
  560. BEGIN WRITE('The First ');WRITE(M:1);
  561. WRITE(' Prime Numbers --- Page ');
  562. WRITE(PAGENUMBER:1);WRITELN;WRITELN;
  563. FOR ROWOFFSET:=PAGEOFFSET TO PAGEOFFSET+RR-1
  564. DO{10:}
  565. BEGIN FOR C:=0 TO CC-1 DO IF ROWOFFSET+C*RR<=
  566. M THEN WRITE(P[ROWOFFSET+C*RR]:WW);WRITELN;
  567. END{:10};PAGE;END{:9};
  568. PAGENUMBER:=PAGENUMBER+1;
  569. PAGEOFFSET:=PAGEOFFSET+RR*CC;END;END{:8}{:3};
  570. END.{:2}{:1}
  571. /endverbatim
  572. \medskip
  573. \caption 3. PASCAL program generated from the \WEB\ file.
  574. \medskip
  575. \hrule
  576. \endfloat
  577. \beginsection F. THE WOVEN OUTPUT
  578. I mentioned earlier that {\tt WEAVE} is a program that converts a file
  579. like {\tt PRIMES.WEB} into a file {\tt PRIMES.TEX} that is a syntactically
  580. correct source file for \TeX. Figure~4 gives a sampling of {\tt PRIMES.TEX},
  581. which is even more unreadable than {\tt PRIMES.PAS}. The instructions that
  582. cause \TeX\ to produce formatted \PASCAL\ programs, with appropriate
  583. typefaces and indentation, etc., are somewhat complex because they are
  584. supposed to give decent results regardless of the page size.
  585. There is no need to discuss Figure~4 further in the present paper, because
  586. the details of ``pretty printing'' are not relevant to my main theme.
  587. I have shown this much of {\tt PRIMES.TEX} only to make the point that
  588. it is nice to have a program like {\tt WEAVE} to do all the formatting;
  589. computer programs are not easy to typeset.
  590. \leftfloat
  591. \ttverbatim
  592. /hrule
  593. /medskip
  594. \input webmac
  595. \font\ninerm=amr9
  596. /cvdots
  597. syntactically correct \PASCAL\ program will
  598. be obtained.\]
  599. /medskip
  600. \Y\P$\4\X2:Program to print the first
  601. thousand prime numbers\X\S$\6
  602. \4\&{program}\1\  \37$\\{print\_primes}(%
  603. \\{output})$;\6
  604. \4\&{const} \37$\|m=1000$;\5
  605. \X5:Other constants of the program\X\6
  606. \4\&{var} \37\X4:Variables of the program\X\6
  607. \&{begin} \37\X3:Print the first \|m prime
  608. numbers\X;\6
  609. \&{end}.\par
  610. \U section~1.\fi
  611. /cvdots
  612. The first three macro definitions here are
  613. parametric; the other two are simple.\]
  614. /medskip
  615. \Y\P\D \37$\\{print\_string}(\#)\S\\{write}(%
  616. \#)$\C{put a given string into the %
  617. \\{output} file}\par
  618. /cvdots
  619. \:{Bertrand, Joseph, postulate}, 21.
  620. \:\\{boolean}, 15.
  621. /cvdots
  622. \:\.{WEB}, 1.
  623. \:\\{write}, 6.
  624. \:\\{write\_ln}, 6.
  625. \:\\{ww}, \[5], 6.
  626. /cvdots
  627. \:\X4, 7, 12, 15, 17, 23, 24:Variables of
  628. the program\X
  629. \U section~2.
  630. /endverbatim
  631. \medskip
  632. \caption 4. \TeX\ program generated from the \WEB\ file.
  633. \medskip
  634. \hrule
  635. \endfloat
  636. \beginsection G. ADDITIONAL BELLS AND WHISTLES
  637. A system like \WEB\ can be successful only if it is capable of handling
  638. large programs as well as small ones, and only if it is complete enough
  639. to take care of all the practical requirements that arise when many
  640. different kinds of programs are considered. A small example like
  641. {\tt PRIMES.WEB} is a satisfactory vehicle for illustrating the general
  642. ideas, but it cannot be convincing as a demonstration of \WEB's ability
  643. to produce quality software in the ``real world.'' My original design
  644. of \WEB\ in September, 1981, was followed by a year of extensive
  645. experiments, so that by the time Version~1 was released in
  646. September, 1982, I could be fairly confident that the language was
  647. reasonably complete. Since then only one or two small extensions
  648. have proved to be necessary; and although numerous enhancements can
  649. easily be imagined, I believe that a useful stopping point for a
  650. working system called {\tt WEB83} has been reached.
  651. A full description of {\tt WEB83} appears in a Stanford report,\ref\WEBman%
  652. {D. E. Knuth, {\sl The \WEB\kern-2pt\ System of Structured Documentation}.
  653. Stanford Computer Science Report CS980 (September 1983).}
  654. which also contains the complete \WEB\ programs for {\tt WEAVE} and
  655. {\tt TANGLE}. The full language contains only a few features that
  656. do not show up in the {\tt PRIMES} example considered above:
  657. \def\nindent#1{\noindent\hbox to\parindent{#1)\hfil}\ignorespaces}
  658. \smallskip
  659. \nindent1 There are facilities to override {\tt WEAVE}'s
  660. automatic formatting of \PASCAL\ programs. For example, it is
  661. possible to force a statement to begin on a new line, or to force
  662. several statements to appear on the same line, or to suggest
  663. a desirable breakpoint in the middle of a long expression. In
  664. unusual cases, {\tt WEAVE} must parse program fragments that
  665. are not syntactically complete---for example, there may be a
  666. {\bf begin} without a matching {\bf end}---so a \WEB\ user must
  667. be given a chance to control the results. Furthermore there is a
  668. facility for changing {\tt WEAVE}'s formatting rules by declaring
  669. that a certain identifier should be treated as a certain \PASCAL\
  670. reserved word, or by declaring that a certain reserved word
  671. should be treated as an ordinary identifier.
  672. \smallskip
  673. \nindent2 There is a way to force {\tt TANGLE} to omit a
  674. space between two adjacent pieces of text, so that a name
  675. like `\\{x3}' can be manufactured from `\|x' and `\\3'. Similarly,
  676. there is a way to pass an arbitrary sequence of characters through
  677. {\tt TANGLE} so that the same sequence will appear ``verbatim'' in
  678. the \PASCAL\ file; and there is a way to force beginning-of-line
  679. in that file. The latter extensions have proved to be necessary
  680. to deal with various nonstandard conventions of different \PASCAL\ compilers.
  681. When a comment in braces is sent to the \PASCAL\ file, {\tt TANGLE}
  682. is careful not to introduce further braces inside the comment.
  683. \smallskip
  684. \nindent3 There are facilities for octal and hexadecimal constants in \WEB\
  685. thees. {\tt TANGLE} converts such constants to decimal form; {\tt WEAVE} gives
  686. them an appropriate typographic treatment.
  687. \smallskip
  688. \nindent4 There is a facility for dealing with alphabetic constants.
  689. When a program contains a double-quoted character like {\tt"A"},
  690. {\tt TANGLE} converts this to an integer between 0 and~127 that
  691. equals the corresponding {\mc ASCII} code (in this case 65).
  692. The use of {\mc ASCII} code facilitates the construction of software
  693. that is readily portable from one machine to another, independent of
  694. the actual character set in use.
  695. \smallskip
  696. \nindent5 Furthermore, if a double-quoted constant is a string
  697. of several characters, like {\tt"cat"}, {\tt TANGLE} converts it
  698. into a unique integer that is 128 or more. A special {\it string pool
  699. file\/} is written, containing all of the strings that have been
  700. specially encoded in this way. I have used this general mechanism only
  701. in large programs, but experience has shown that it makes quite a
  702. nice substitute for the string-processing capabilities that \PASCAL\
  703. lacks. (Incidentally, I noticed after several months that a program
  704. needs to have some indication that the string-pool file it is reading
  705. contains the same strings that {\tt TANGLE} generated when the program
  706. itself was tangled. Therefore a ``check sum'' is included in the
  707. string pool file; each program is able to refer to its own check sum
  708. and to compare it with the value in the file. This check-sum extension
  709. was one of the last features to be added to \WEB.)
  710. \smallskip
  711. \nindent6 The {\tt PRIMES} example illustrates macros with
  712. parameters and macros without parameters. \WEB\ also allows ``numeric''
  713. macros, which are small integer constants; {\tt TANGLE} is capable of
  714. doing simple arithmetic on such constants. This feature of \WEB\ was
  715. introduced specifically to overcome \PASCAL's unfortunate inability to
  716. do compile-time arithmetic. For example, it is impossible to have a
  717. \PASCAL\ array whose bounds are `$0\to n-1$', or to write
  718. `$20+3:$' as the label of one of the cases in `{\bf case} $x+y$';
  719. \WEB's numeric macros make it possible for {\tt TANGLE} to
  720. preprocess such constants.
  721. \beginsection H. OCCAM'S RAZOR
  722. I would also like to mention several things that were intentionally left
  723. out of \WEB, since I have tried to keep the language as simple as I could.
  724. There are no ``conditional macros,'' nor does {\tt TANGLE}
  725. evaluate Boolean expressions that might influence the
  726. output. I~found that everything I needed could be done
  727. satisfactorily by commenting out the optional code.
  728. For example, a system program is often designed to gather statistics about
  729. its own operation, but such statistics-gathering is pointless unless someone
  730. is actually going to use the results. In order to make the instrumentation
  731. code optional, I include the word `{\bf stat}' just before any special
  732. code for statistics, and `{\bf tats}' just after such code; and I tell
  733. {\tt WEAVE} to regard {\bf stat} and {\bf tats} as if they were {\bf begin}
  734. and {\bf end}. But {\bf stat} and {\bf tats} are actually simple macros.
  735. When I do want to gather the statistics, I define {\bf stat} and {\bf tats} to
  736. be null; but in a production version of the software, I make {\bf stat}
  737. expand to~`{\tt@\char`\{}' and {\bf tats} expand to~`{\tt@\char`\}}',
  738. where {\tt@\char`\{} and {\tt@\char`\}} are special braces that {\tt TANGLE}
  739. does not remove. Thus the optional code appears as a harmless comment in
  740. the \PASCAL\ program.
  741. \WEB's macros are allowed to have at most one parameter. Again, I did this
  742. in the interests of simplicity, because I noticed that most applications
  743. of multiple parameters could in fact be reduced to the one-parameter case.
  744. For example, suppose that you want to define something like
  745. $$\hbox{\tt mac(\#1,\#2) == m[\#1*r+\#2]}$$
  746. which \WEB\ doesn't permit. You can get essentially the same result
  747. with two one-parameter macros
  748. $$\vbox{\halign{\tt#\hfil\cr
  749. mac\char`\_tail(\#) == \#]\cr
  750. mac(\#) == m[\#*r+mac\char`\_tail\cr}}$$
  751. since, e.g., `{\tt mac(a)(b)}' will expand into `{\tt m[a*r+b]}'.
  752. Here is another example that indicates some of the surprising generality
  753. of one-parameter macros: Consider the two definitions
  754. $$\vbox{\halign{\tt#\hfil\cr
  755. define two\char`\_cases(\#)==case j of\cr
  756. \ \ \ \ \ \ \ \ \ \ \ \ \ 1:\#(1); 2:\#(2); end\cr
  757. define reset\char`\_file(\#)==reset(file@\&\#)\cr}}$$
  758. where `{\tt@\char`\&}' in the second definition is the concatenation operation
  759. that pastes two texts together. You can now say
  760. $$\hbox{\tt two\char`\_cases(reset\char`\_file)}$$
  761. and the resulting \PASCAL\ output will be
  762. $$\vbox{\halign{\tt#\hfil\cr
  763. case j of\cr
  764. 1:reset(file1);\cr
  765. 2:reset(file2);\cr
  766. end\cr}}$$
  767. In other words, the name of one macro can usefully be a parameter to
  768. another macro. This particular trick makes it possible to live with
  769. \PASCAL\ compilers that do not allow arrays of files.
  770. \beginsection I. PORTABILITY
  771. One of the goals of my \TeX\ research has been to produce portable
  772. software, and the {\tt WEB} system has been extremely helpful in this
  773. respect. Although my own work is done on a DEC-10 computer with
  774. Stanford's one-of-a-kind operating system, the software developed
  775. with \WEB\ has already been transported successfully to a wide
  776. variety of computers made by other manufacturers (including IBM,
  777. Control Data, XEROX, Hewlett-Packard), and to a variety
  778. of different operating systems for those machines. To my knowledge,
  779. no other software of such complexity has ever been transported to
  780. so many different machines. It seems likely that \TeX\ will soon be
  781. operating on all but the smallest of the world's computer systems.
  782. To my surprise, the main bottleneck to portability of the \TeX ware
  783. has been the lack of suitable \PASCAL\ compilers, because \PASCAL\ has
  784. often been implemented without system programming in mind. Anybody
  785. who has a decent \PASCAL\ compiler can install \WEB\ (and all programs
  786. written in \WEB) without great difficulty, essentially as follows:
  787. \smallskip
  788. \item{1)} Start with the three files {\tt WEAVE.WEB}, {\tt TANGLE.WEB}, and
  789. {\tt TANGLE.PAS}. (The programs have not been copyrighted, so
  790. these files are not difficult to obtain.)
  791. \item{2)} Run {\tt TANGLE.PAS} through your \PASCAL\ compiler to
  792. get a working {\tt TANGLE} program.
  793. \item{3)} Check your {\tt TANGLE} by applying it to {\tt TANGLE.WEB};
  794. your output file should match {\tt TANGLE.PAS}.
  795. \item{4)} Apply your {\tt TANGLE} to the file {\tt WEAVE.WEB}, obtaining
  796. {\tt WEAVE.PAS}; then apply \PASCAL\ to {\tt WEAVE.PAS} and you'll
  797. have a working {\tt WEAVE} system.
  798. \item{5)} The same process applies to any software written in \WEB,
  799. notably to \TeX\ itself. (However, you need fonts and suitable output
  800. equipment in order to make proper use of \TeX; that may be another
  801. bottleneck.) Once you have \TeX\ working, you can apply {\tt WEAVE}
  802. and \TeX\ to your \WEB\ files, thereby getting program documents
  803. as illustrated above.
  804. \smallskip\noindent
  805. Notice that a {\tt TANGLE.PAS} file is needed in order to get this
  806. ``bootstrapping'' process started. If you have just {\tt WEAVE.WEB}
  807. and {\tt TANGLE.WEB}, you can't do the first step.
  808. However, anybody who has looked seriously into the question of software
  809. portability will realize that my comments in the preceding paragraphs
  810. have been oversimplified. I have glossed over some serious
  811. problems that arise: Character sets are different; file naming conventions
  812. are different; special conventions are needed to interact with a user's
  813. terminal; data is packed differently on different machines; floating-point
  814. arithmetic is always nonstandard and sometimes nonexistent; users want
  815. ``friendly'' interaction with existing programs for editing and spooling;
  816. etc., etc. Furthermore, many of the world's \PASCAL\ compilers are incredibly
  817. bizarre. Therefore it is quite na\"\i ve to believe that a single program
  818. {\tt TANGLE.PAS} could actually work on very many different machines, or
  819. even that one single source file {\tt TANGLE.WEB} could be adequate; some
  820. system-dependent\kern-.5pt\kern.5pt\ changes are inevitable.
  821. The \WEB\ system caters to system-dependent changes in a simple but surprisingly
  822. effective way that I neglected to mention when I listed its other features.
  823. Both {\tt TANGLE} and {\tt WEAVE} are designed to work with
  824. {\it two\/} input files, not just one: In addition to a \WEB\ source file
  825. like {\tt TEX.WEB}, there is also a ``change file'' {\tt TEX.CH} that
  826. contains whatever changes are needed to customize \TeX\ for a particular
  827. system. (Similarly, the source files {\tt WEAVE.WEB} and {\tt TANGLE.WEB}
  828. are accompanied by {\tt WEAVE.CH} and {\tt TANGLE.CH}.)
  829. Here's how change files work: Each change has the form ``replace
  830. $x_1\ldots x_m$ by $y_1\ldots y_n$,'' for some $m\ge 1$ and $n\ge0$;
  831. here $x_i$ and~$y_j$ represent lines in the change file.
  832. The {\tt WEAVE} and {\tt TANGLE} programs
  833. read data from the \WEB\ input file until finding a line that matches
  834. $x_1$; this line, and the $m-1$ following lines, are replaced by
  835. $y_1\ldots y_n$.  An error message is given if the $m$ lines replaced did
  836. not match $x_1\ldots x_m$ perfectly.
  837. For example, the program {\tt PRIMES.WEB} invokes a \\{page} procedure to
  838. begin a new page; but \\{page} was not pres\-ent in Wirth's original \PASCAL\
  839. and it is defined rather vaguely in the \PASCAL\ standard. Therefore
  840. a system-dependent change may be needed here. A change file {\tt
  841. PRIMES.CH} could be made by copying the line
  842. $$\hbox{\tt @d new\char`\_page==page}$$ from
  843. Figure~2c and specifying one or more appropriate replacement lines.
  844. The program {\tt TANGLE} itself contains about 190 sections, and a
  845. typical installation will have to change about 15 of these. If you
  846. want to transport {\tt TANGLE} to a new environment, you therefore
  847. need to create a suitable file {\tt TANGLE.CH} that modifies 15~or~so parts
  848. of {\tt TANGLE.WEB}. (Examples of {\tt TANGLE.CH} are provided to
  849. all people who receive {\tt TANGLE.WEB}, so that each implementor has
  850. a model of what to do.) You need to insert your changes by hand into
  851. {\tt TANGLE.PAS}, until you have a {\tt TANGLE} program that works
  852. sufficiently well to support further bootstrapping. But you never
  853. actually change the master file {\tt TANGLE.WEB}.
  854. This approach has two important advantages. First, the same
  855. master file {\tt TANGLE.WEB} is used by everybody, and it
  856. contains the basic logic of {\tt TANGLE} that really defines the
  857. essence of tangling. The system-dependent changes do not affect
  858. any of the subtle parts of {\tt TANGLE}'s control structures or
  859. data structures. Second, when the official {\tt TANGLE} has been
  860. upgraded to a newer version, a brand new {\tt TANGLE.WEB} will
  861. almost always work with the old {\tt TANGLE.CH}, since changes
  862. are rarely made to the system-dependent parts. In other words,
  863. this dual-input-file scheme works when the \WEB\ file is constant
  864. and the {\tt CH} file is modified, and it also works when the
  865. {\tt CH} file is constant but the \WEB\ file is modified.
  866. Change files were added to \WEB\ about three months after the system was
  867. initially designed, based on our initial experiences with people who had
  868. volunteered to participate in portability experiments. We realized
  869. about a year later that {\tt WEAVE} could be modified so that
  870. only the changed parts of a program would (optionally) be printed; thus,
  871. it's now possible to document the changes by listing only the sections
  872. that are actually affected by the {\tt CH} file that {\tt WEAVE} has
  873. processed. We also generalized the original format of {\tt CH} files,
  874. which permitted only changes that extended to the end of a section. These
  875. two important ideas were among the final enhancements incorporated into
  876. {\tt WEB83}.
  877. \beginsection J. PROGRAMS AS WEBS
  878. When I first began to work with the ideas that eventually became the
  879. \WEB\ system, I thought that I would be designing a language for ``top-down''
  880. programming, where a top-level description is given first and successively
  881. refined. On the other hand I knew that I often created major parts
  882. of programs in a ``bottom-up'' fashion, starting with the definitions of
  883. basic procedures and data structures and gradually building more and
  884. more powerful subroutines. I had the feeling that top-down and bottom-up
  885. were opposing methodologies: one more suitable for program exposition
  886. and the other more suitable for program creation.
  887. But after gaining experience with \WEB, I have come to realize that there is
  888. no need to choose once and for all between top-down and bottom-up, because
  889. a program is best thought of as a web instead of a tree. A hierarchical
  890. structure is present, but the most important thing about a program is
  891. its structural relationships. A complex piece of software consists of
  892. simple parts and simple relations between those parts; the programmer's
  893. task is to state those parts and those relationships, in whatever order
  894. is best for human comprehension---not in some rigidly determined
  895. order like top-down or bottom-up.
  896. When I'm writing a longish program like {\tt TANGLE.WEB} or {\tt WEAVE.WEB}
  897. or {\tt TEX.WEB}, I invariably have strong feelings about what part of the
  898. whole should be tackled next. For example, I'll come to a point where I need
  899. to define a major data structure and its conventions, before I'll feel
  900. happy about going further. My experiences have led me to believe that a
  901. person reading a program is, likewise, ready to comprehend it by
  902. learning its various parts in approximately the order in which it
  903. was written. The {\tt PRIMES.WEB} example illustrates this principle on
  904. a small scale; the decisions that Dijkstra made as he composed the original
  905. program$^\Dijk$ appear in the \WEB\ documentation in the same order.
  906. Top-down programming gives you a strong idea of where you are going, but
  907. it forces you to keep a lot of plans in your head; suspense builds up
  908. because nothing is really nailed down until the end. Bottom-up programming
  909. has the advantage that you continually wield a more and more powerful
  910. pencil, as more and more subroutines have been constructed; but it forces
  911. you to postpone the overall program organization until the last minute,
  912. so you might flounder aimlessly.
  913. When I tear up the first draft of a program and start over, my second draft
  914. usually considers things in almost the same order as the first one did.
  915. Sometimes the ``correct'' order is top-down, sometimes it is bottom-up,
  916. and sometimes it's a mixture; but always it's an order that makes sense on
  917. expository grounds.
  918. Thus the \WEB\ language allows a person to express programs
  919. in a ``stream of consciousness'' order. {\tt TANGLE} is able to scramble
  920. everything up into the arrangement that a \PASCAL\ compiler demands. This
  921. feature of \WEB\ is perhaps its greatest asset; it makes a \WEB-written
  922. program much more readable than the same program written purely in
  923. \PASCAL, even if the latter program is well commented. And the fact that there's
  924. no need to be hung up on the question of top-down versus bottom-up---since
  925. a programmer can now view a large program as a web, to be explored in
  926. a psychologically correct order---is perhaps the greatest lesson I have
  927. learned from my recent experiences.
  928. Another surprising thing that I learned while using \WEB\ was that
  929. traditional programming languages had been causing me to write inferior
  930. programs, although I hadn't realized what I was doing. My original idea was that
  931. \WEB\ would be merely a tool for documentation, but I actually found that
  932. my \WEB\ programs were better than the programs I had been writing
  933. in other languages. How could this be?
  934. Well, imagine that you are writing a small subroutine that updates part
  935. of a data structure, and suppose that the updating takes only
  936. one or two lines of code. In practical programs, there's often something
  937. that can go wrong, if the user's input is incorrect, so the subroutine
  938. has to check that the input is correct before doing the update.
  939. Thus, the subroutine has the general form
  940. $$\vbox{\halign{#\hfil\cr
  941. \&{procedure} \\{update};\cr
  942. \&{begin if} \<input data is invalid\> \&{then}\cr
  943. \quad \<Issue an error message and try to recover\>;\cr
  944. \<Update the data structure\>;\cr
  945. \&{end}.\cr}}$$
  946. A subtle phenomenon occurs in traditional programming languages: While
  947. writing the program for `\<Issue an error message and try to recover\>',
  948. a programmer subconsciously tries to get by with the fewest possible
  949. lines of code, since the program for `\<Update the data structure\>' is
  950. quite short. If an extensive error recovery is actually programmed, the
  951. subroutine will appear to have error-message printing as its main purpose.
  952. But the programmer knows that the error is really an exceptional case that
  953. arises only rarely; therefore a lengthy error recovery doesn't look right,
  954. and most programmers will minimize it (without realizing that they are doing
  955. so) in order to make the subroutine's appearance match its intended behavior.
  956. On the other hand when the same task is programmed with \WEB, the purpose
  957. of \\{update} can be shown quite clearly, and the possibility of error
  958. recovery can be reduced to a mere mention when \\{update} is defined.
  959. When another section entitled `\<Issue an error message and try to
  960. recover\>' is subsequently written, the whole point of that section is to do
  961. the best error recovery, and it becomes quite natural to write a better program
  962. as a result.
  963. This fact---that \WEB\ allows you to let each part of the program have
  964. its appropriate size, without distorting the readability of other parts---means
  965. that good programmers find their \WEB\ programs better than their \PASCAL\
  966. programs, even though their \PASCAL\ programs once looked like the work
  967. of an expert.
  968. \beginsection K. STYLISTIC ISSUES
  969. I found that my style of using \WEB\ evolved quite a bit during the first
  970. year. The general format, in which each section beings with commentary and
  971. ends with a formal program fragment, is extremely versatile; you have the
  972. freedom to say anything you want, yet you must make a decision about how
  973. you'll do it.  I imagine that different programmers will converge to
  974. quite different styles, but I would like to note down some of the things
  975. that have seemed to work best for me.
  976. Consider first the question of macros versus section names. A named section,
  977. like `\<Issue an error message and try to recover\>', is essentially the
  978. same as a parameterless macro; \WEB\ provides both. I prefer to use
  979. parameterless macros for ``small'' things that can be embodied in a word
  980. or two, but named sections for longer portions of the program that
  981. merit a fuller description.
  982. I usually start the name of a section with an imperative verb, but I give
  983. a declarative commentary at the beginning of a section. Thus,
  984. {\tt PRIMES.WEB} says `{\bf 8.}~Now that appropriate $\ldots$
  985. \X8:Print table $p$\X$\;\S\;$\dots\thinspace'; I wouldn't do the opposite
  986. and say `{\bf8.}~Print the table. \X8:Code for printing\X$\;\S\;$\dots'.
  987. The name of a section (enclosed in angle brackets) should be long enough
  988. to encapsulate the essential characteristics of the code in that section,
  989. but it should not be too verbose. I found very early that it would be a
  990. mistake to include all of the assumptions about local and global variables
  991. in the name of each section, even though such information would strictly
  992. be necessary to isolate that section as an independent module. The trick is
  993. to find a balance between formal and informal exposition so that a reader
  994. can grasp what is happening without being overwhelmed with detail.\ref\Naur%
  995. {P. Naur, Formalization in program development. {\sl BIT\/ \bf22},
  996. 437--453 (1982).}
  997. Another lesson I learned early in the game was that the name of a section
  998. should explicitly mention any nonstandard control structures, even though
  999. its data structures can often be left implied. Furthermore, if the control
  1000. flow is properly explained, you can avoid the usual errors associated
  1001. with \&{goto} statements; such statements can safely be introduced in
  1002. a restrained but natural manner.
  1003. For example, \sec14 of the prime-printing example could be reprogrammed as
  1004. follows, using `\&{loop}' as a macro abbreviation for `\&{while} \\{true}
  1005. \&{do}':
  1006. $$\vbox{\halign{\hbox to\hsize{#\hfil}\cr
  1007. \X14:Increase $j$ until it is the next prime number\X$\;\S$\cr
  1008. \quad\&{loop begin} $j\K j+2$;\cr
  1009. \qquad\X20:Update variables that depend on $j$\X;\cr
  1010. \qquad\X22:If $j$ is prime, \&{goto} \\{found}\X;\cr
  1011. \qquad\&{end};\cr
  1012. \\{found}:\cr}}$$
  1013. With this change, \sec22 could become
  1014. $$\vbox{\halign{\hbox to\hsize{#\hfil}\cr
  1015. \X22:If $j$ is prime, \&{goto} \\{found}\X$\;\S$\cr
  1016. \quad$n\K2$;\cr
  1017. \quad\&{while} $n<\\{ord}$ \&{do}\cr
  1018. \qquad\&{begin} \X26:If $p[n]$ is a factor of $j$, \&{goto} \\{not\_found}\X;\cr
  1019. \qquad$n\K n+1$;\cr
  1020. \qquad\&{end};\cr
  1021. \quad\&{goto} \\{found};\cr
  1022. \\{not\_found}:\cr}}$$
  1023. if \sec26 changes in the obvious way. The resulting program will be more
  1024. efficient on most machines; and I believe that it is actually easier to
  1025. read and to write, in spite of the fact that two \&{goto} statements
  1026. appear, because the labels have been used with appropriate interpretations
  1027. of their abstract significance.
  1028. Of course, \PASCAL\ makes it difficult to use \&{goto} statements,
  1029. because Wirth decided that labels should be numeric, and that they
  1030. should be declared in advance. If I were to introduce the \&{goto}
  1031. statements as suggested, I would have to define numeric macros
  1032. \\{found} and \\{not\_found}, and I would have to insert
  1033. `\&{label} \\{found}, \\{not\_found}' into the program at the right place.
  1034. Such extra work is a bit of a nuisance, but it can be done in \WEB\ without
  1035. spoiling the exposition.
  1036. \PASCAL\ has a few other misfeatures that prove to be inconvenient with
  1037. respect to \WEB\ exposition. The worst of these is the inability to
  1038. declare local variables in the midst of a program or procedure. For
  1039. example, a programmer often finds it most natural to define an integer
  1040. variable when a \&{for} loop is introduced, but the rules of \PASCAL\
  1041. insist that such a variable be declared rather far away from
  1042. that \&{for} loop. My \WEB\ programs overcome this problem by having
  1043. sections like `\<Local variables for \\{xyzzy}\>' whenever there's a
  1044. rather lengthy procedure `\\{xyzzy}' whose local variables should not
  1045. be declared all at once. But when a procedure is short, say only half
  1046. a dozen sections long, there's usually no harm in declaring its local
  1047. variables in \PASCAL\ style, because the entire text of the procedure will
  1048. tend to appear on one or two adjacent pages of the documentation.
  1049. Another slightly awkward aspect of \PASCAL\ is its treatment of semicolons.
  1050. If you look closely at the prime-number example, you'll see that I had to
  1051. be a bit careful about where I put semicolons; sometimes they occur at the
  1052. end of the expanded text of a section, but usually they don't. With
  1053. a little self discipline, a person can learn to do this quite satisfactorily,
  1054. but it is a nuisance until you get used to it.
  1055. \beginsection L. ECONOMIC ISSUES
  1056. What does it cost to use \WEB? Let's look first at the lowest level, where
  1057. computer costs are considered, because it is easy to make quantitative
  1058. statements at this level. The running time to {\tt TANGLE} a \WEB\ file is
  1059. approximately the same as the time needed to compile the resulting
  1060. \PASCAL\ program; hence the extra preprocessing does not cost much.
  1061. Similarly, {\tt WEAVE} doesn't take long to produce a file for \TeX.
  1062. However, \TeX\ needs a comparatively large amount of time to typeset the
  1063. final document. For example, if we assume that each page requires four
  1064. seconds, it will take four minutes to produce a 60-page document. The
  1065. running time for {\tt WEAVE}-plus-\TeX\ is quite reasonable when you
  1066. consider that your program is effectively being
  1067. converted into a fairly substantial booklet; but the costs are sufficiently
  1068. large to discourage remaking and reprinting such a booklet more than once or
  1069. twice a day. When a new program is being developed, it is therefore customary
  1070. to work with hardcopy documentation that is slightly obsolete, and to read
  1071. the \WEB\ source file itself when up-to-date information is required;
  1072. the source file is sufficiently easy to read for such purposes.
  1073. The costs of \WEB\ are more difficult to estimate at higher levels, but I have
  1074. found to my surprise that the total time of writing and debugging a \WEB\
  1075. program is no greater than the total time of writing and debugging an
  1076. {\mc ALGOL} or {\mc PASCAL} program, even though my \WEB\ programs are
  1077. much better, and even though I am putting substantially more documentation
  1078. into the programs. Therefore I have lately been using \WEB\ for all of my
  1079. programming, even for one-off jobs that I write ``for my eyes only'' just
  1080. to explore occasional problems. The extra time I spend in preparing additional
  1081. commentary is regained because the debugging time is reduced.
  1082. In retrospect, the fact that a ``literate'' program takes much less time to
  1083. debug is not surprising, because the \WEB\ language encourages a discipline
  1084. that I was previously unwilling to impose on myself. I had known for a long
  1085. time that the programs I construct for publication in a book, or the programs
  1086. that I construct in front of a class, have tended to be comparatively free
  1087. of errors, because I am forced to clarify my thoughts as I do the programming.
  1088. By contrast, when writing for myself alone, I have often taken shortcuts that
  1089. proved later to be dreadful mistakes. It's harder for me to fool myself in
  1090. such ways when I'm writing a \WEB\ program, because I'm in ``expository
  1091. mode'' (analogous to classroom lecturing) whenever a \WEB\ is being spun.
  1092. Ergo, less debugging time.
  1093. Now that I am writing all my programs in \WEB, an unforeseen problem has,
  1094. however, arisen: I suddenly have a collection of programs that seem quite
  1095. beautiful in my own eyes, and I have a compelling urge to publish all of
  1096. them so that everybody can admire these works of art. A nice little 10-page
  1097. program can easily be written and debugged in an afternoon and evening;
  1098. if I keep accumulating such gems, I'll soon run out of storage space,
  1099. and my office will be encrusted with webs of my own making. There is no
  1100. telling what will happen if lots of other people catch \WEB\ fever and
  1101. start foisting their creations on each other. I can already envision the
  1102. appearance of a new journal, to be entitled {\sl Webs}, for the publication
  1103. of literate programs; I imagine that it will have a large backlog and
  1104. a large group of dedicated editors and referees.
  1105. \beginsection M. RELATED WORK
  1106. Nothing about \WEB\ is really new; I have simply combined a bunch of
  1107. ideas that have been in the air for a long time. I would like to
  1108. summarize in the next few paragraphs the things that had the greatest
  1109. influence on my thinking as I put those pieces together.
  1110. George Forsythe wrote in 1966 that ``A useful algorithm is a substantial
  1111. contribution to knowledge. Its publication constitutes an important
  1112. piece of schol\-ar\-ship.''\ref\GEF{G. E. Forsythe, Algorithms for
  1113. scientific computation. {\sl Communications of the ACM\/ \bf9}, 255--256
  1114. (1966).} His comments have always inspired me to strive for excellence
  1115. in programming, and they have played a major r\^^Dole in shaping my present
  1116. view that it is worthwhile to consider {\it every\/} program as a
  1117. work of literature.
  1118. The design of \WEB\ was influenced primarily by the pioneering work
  1119. of Pierre-Arnoul de Marneffe,\ref\deM{P. A. de Marneffe, {\sl Holon
  1120. Programming}. Univ.~de Liege, Service D'Informatique (December, 1973).}$^,$%
  1121. \ref\deMR{P. A. de Marneffe and D. Ribbens, Holon Programming, in
  1122. A. G\"unther et al.\ (eds.), {\sl International Computing Symposium 1973\/},
  1123. Amsterdam, North-Holland (1974).} whose research on what he called
  1124. ``Holon Programming'' has not received the attention it deserves. His
  1125. work was, in turn, inspired by Arthur Koestler's excellent treatise
  1126. on the structure of complex systems and organisms;\ref\Koest{A.
  1127. Koestler, {\sl The Ghost in the Machine}. New York, Macmillan (1968).}
  1128. thus we have another connection between programming and literature.
  1129. A somewhat similar system was independently created by Edwin Towster.\ref\Tow%
  1130. {E. Towster, A convention for explicit declaration of environments
  1131. and top-down refinement of data. {\sl IEEE Transactions on Software
  1132. Engineering\/ \bf SE--5}, 374--386 (1979).}
  1133. I owe a great debt to Edsger Dijkstra, Tony Hoare, Ole-Johan Dahl, and
  1134. Niklaus Wirth for opening my eyes to the importance of abstraction in the
  1135. reading and writing of programs, and to Peter Naur for stressing the
  1136. importance of a balance between formal and informal methods.
  1137. Tony Hoare provided a special impetus for \WEB\ when he suggested in 1978
  1138. that I should publish my program for \TeX. Since very few large-scale
  1139. software systems were available in the literature, he had been trying to
  1140. promote the publication of well-written programs. Hoare's suggestion was
  1141. actually rather terrifying to me, and I'm sure he knew that he was posing
  1142. quite a challenge. As a professor of computer science, I was quite
  1143. comfortable publishing papers about toy problems that could be polished up
  1144. nicely and presented in an elegant manner; but I had no idea how to take a
  1145. piece of real software, with all the compromises necessary to make it
  1146. useful to a large class of people on a wide variety of systems, and to open
  1147. it up to public scrutiny. How could a supposedly respectable academic, like
  1148. me, reveal the way he actually writes large programs? And could a large
  1149. program be made intelligible? My previous attempts along these
  1150. lines\ref\CF{D. E. Knuth, Computer-drawn flow charts.  {\sl
  1151. Communications of the ACM\/ \bf 6}, 555--563 (1963).} were by now
  1152. hopelessly out of date.  I decided that this would be a good time to try
  1153. out de Marneffe's ideas; furthermore, the \TeX\ system itself provided me
  1154. with new tools for printing and format control, so I suspected that it
  1155. would be possible to obtain state-of-the-art documentation by making
  1156. proper use of typography.
  1157. It is interesting to reread some of the comments that Tony made ten years
  1158. ago in his keynote address to the first ACM symposium on Principles
  1159. of Programming Languages:\ref\Hoare{C. A. R. Hoare, {\sl Hints on
  1160. Programming Language Design}. Stanford Computer Science Report CS403
  1161. (October 1973).}
  1162. \smallskip
  1163. {\narrower\noindent
  1164. Documentation must be regarded as an integral part of the process of
  1165. design and coding. A good programming language will encourage and
  1166. assist the programmer to write clear, self-documenting code, and even
  1167. perhaps to develop and display a pleasant style of writing.
  1168. \smallskip}
  1169. \noindent He foresaw many future trends, but not the impending improvements in
  1170. typesetting quality:
  1171. \smallskip
  1172. {\narrower\noindent
  1173. It is of course possible for a compiler or service program to expand the
  1174. abbreviations, fill in the defaults, and make explicit the assumptions.
  1175. But in practice, experience shows that it is very unlikely that the
  1176. output of a computer will ever be more readable than its input, except
  1177. in such trivial but important aspects as improved indentation.
  1178. \smallskip}
  1179. Typographic formatting of computer programs has a long tradition, originating
  1180. with {\mc ALGOL} and its immediate precursors. I'm not sure who made the
  1181. first experiments, but I believe that the lion's share of the credit
  1182. for developing excellent programming-language typography belongs to two
  1183. people: Peter Naur, who edited the {\mc ALGOL~60} report\ref\Alg{P. Naur
  1184. (ed.)~et al., Report on the algorithmic language ALGOL 60.
  1185. {\sl Communications of the ACM\/ \bf3}, 299--314.} and gave special
  1186. care to its presentation; and Myrtle Kellington, who served for many years
  1187. as executive editor of ACM publications and set the standards that have
  1188. been adopted by other journals. The computing profession owes much to
  1189. these people, who made published programs so much more readable than they
  1190. would otherwise have been; the magnitude of their contribution can only
  1191. be appreciated by people who submit computer programs to journals
  1192. like {\sl Acta Arithmetica\/} whose editors are unfamiliar with computer
  1193. science. Bill McKeeman called attention to formatting issues when he
  1194. published Algorithm~268, ``{\mc ALGOL~60} reference language editor,''
  1195. in 1965.\ref\McK{W. M. McKeeman, Algorithm 268. {\sl Communications
  1196. of the ACM\/ \bf8}, 667--668 (1965).} There has been a flowering of
  1197. such algorithms in recent years; the papers by Oppen\ref\DO{D. Oppen,
  1198. Prettyprinting. {\sl ACM Transactions on Programming Languages and
  1199. Systems\/ \bf2}, 465--483 (1980).} and by Rose and Welsh\ref\RW{G. A.
  1200. Rose and J. Welsh, Formatted programming languages. {\sl Software---%
  1201. Practice \char'46\ Experience\/ \bf11}, 651--669 (1981).} are particularly
  1202. noteworthy.
  1203. I began to design \WEB\ in the spring of 1979, when I constructed a prototype
  1204. system that was called {\tt DOC}. Luis Trabb~Pardo helped me to develop
  1205. a suitable style of exposition at that time; then Ignacio Zabala~Salelles
  1206. gave a {\tt DOC} a thorough test when he prepared a full implementation
  1207. of \TeX\ in \PASCAL. Zabala's implementation was successfully transported
  1208. to many different computers,\ref\Z{I. Zabala and L. Trabb Pardo,
  1209. The status of the PASCAL implementation of \TeX. {\sl TUGboat\/ \bf1},
  1210. 16--17 (1980).}\silentref\ZZ{I. Zabala, \TeX-PASCAL and PASCAL compilers.
  1211. {\sl TUGboat\/ \bf2} (1), 11--12 (1981).}\silentref\ZZZ{I. Zabala,
  1212. Some feedback from PTEX installations. {\sl TUGboat\/ \bf2} (2), 16--19
  1213. (1981).}$^-$\ref\ZZZZ{I. A. Zabala, How
  1214. portable is PASCAL? Draft of paper in preparation (1982).}
  1215. and this experience was of immense value to me when I cast \WEB\ into its
  1216. present form in 1981. Since then many significant improvements have been
  1217. suggested by my colleague David R. Fuchs, and I have also benefited from
  1218. the experiences of a large number of outstanding people who volunteered to
  1219. be guinea pigs for pre-released versions of \TeX. It's impossible for me
  1220. to name everyone who has helped, but I would like to give special thanks
  1221. to Arthur Samuel, Howard Trickey, Joe Weening, and Pierre MacKay for
  1222. important contributions. I'm fortunate indeed to share a working
  1223. environment with such stimulating people.
  1224. When I originally designed the \WEB\ system, I spent about six
  1225. weeks preparing the files {\tt TANGLE.WEB} and {\tt WEAVE.WEB},
  1226. during which time I was continually changing the language and
  1227. trying different styles of exposition. (The programs were neither
  1228. long nor complicated, but this was rather intensive work, so I
  1229. didn't get much else done during those six weeks. The first two
  1230. weeks were actually spent drafting the first ten per cent of what
  1231. is now {\tt TEX.WEB}.) Then I spent about six tedious hours with
  1232. a text editor, hand-simulating the behavior of {\tt TANGLE} on
  1233. {\tt TANGLE.WEB}, so that I had a program {\tt TANGLE.PAS} that
  1234. was ripe for debugging.  At first I had to correct errors both in
  1235. {\tt TANGLE.WEB} and {\tt TANGLE.PAS}, but soon {\tt TANGLE} was
  1236. working well enough that I needed only {\tt TANGLE.WEB} as a
  1237. source file. Then {\tt WEAVE.WEB} could be tangled and debugged
  1238. too. The total time to create ``Version~0'' of the \WEB\ system,
  1239. including the language design and the time to debug the programs
  1240. and write a brief manual for users, was about eight weeks; then
  1241. enhancements were added at the rate of about one per month for
  1242. the next 18 months.  As a result of this experience I think it's
  1243. reasonable to state that a {\tt WEB}-like system can be created
  1244. from scratch in a fairly short time, for some other pair of
  1245. languages besides \TeX\ and \PASCAL, by an expert system
  1246. programmer who is conversant with both languages. Indeed, I spoke
  1247. about \WEB\ on a recent visit to London and one of the people in
  1248. the audience decided to test this hypothesis; shortly afterwards I
  1249. received an elegant report from Harold Thimbleby, who had just constructed
  1250. an excellent system called {\tt Cweb}, based on Troff/Nroff and {\mc
  1251. C} instead of \TeX\ and \PASCAL.\ref\Thim{H. Thimbleby, {\sl Cweb}.
  1252. Preprint, University of York (August 1983).}
  1253. \beginsection N. RETROSPECT AND PROSPECTS
  1254. Enthusiastic reports about new computer languages, by the authors of those
  1255. languages, are commonplace. Hence I'm well aware of the fact that my own
  1256. experiences cannot be extrapolated too far. I also realize that, whenever I have
  1257. encountered a problem with \WEB, I've simply changed
  1258. the system; other users of \WEB\ cannot operate under the same ground rules.
  1259. However, I believe that I have stumbled on a way of programming that produces
  1260. better programs that are more port\-able and more easily understood and
  1261. maintained; furthermore, the system seems to work with large programs as
  1262. well as with small ones. I'm pleased that my work on typography, which
  1263. began as an application of computers to another field, has come full circle
  1264. and become an application of typography to the heart of
  1265. computer science; I like to think of \WEB\ as a neat ``spinoff'' of my
  1266. research on \TeX. However, all of my experiences with this system have
  1267. been highly colored by my own tastes, and only time will tell if a large
  1268. number of other people will find \WEB\ to be equally attractive and useful.
  1269. I made a conscious decision not to design a language that would be
  1270. suitable for everybody. My goal was to provide a tool for system
  1271. programmers, not for high school students or for hobbyists. I don't have
  1272. anything against high school students and hobbyists, but I don't believe
  1273. every computer language should attempt to offer all things to all people.
  1274. A user of \WEB\ needs to be good enough at computer science that he or she
  1275. is comfortable dealing with several languages simultaneously. Since
  1276. \WEB\ combines \TeX\ and \PASCAL\ with a few rules of its own, \WEB\ programs
  1277. can contain \WEB\ syntax errors, \TeX\ syntax errors, \PASCAL\ syntax errors,
  1278. and algorithmic errors; in practice, all four types of errors occur, and
  1279. a bit of sophistication is needed to sort out which is which. Computer
  1280. scientists tend to be better at such things than other people. I have found
  1281. that \WEB\ programs can be debugged rapidly in spite of the profusion
  1282. of languages, but I'm sure that many other intelligent people will find
  1283. such a task difficult.
  1284. In other words, \WEB\ seems to be specifically for the peculiar breed of
  1285. people who are called computer scientists. And I'm pretty sure that there
  1286. are also a lot of computer scientists who will not enjoy using \WEB; some
  1287. of us are glad that traditional programming languages have comparatively
  1288. primitive capabilities for inserted comments, because such difficulties provide
  1289. a good excuse for not documenting programs well. Thus, \WEB\ may be only for the
  1290. subset of computer scientists who like to write and to explain what they
  1291. are doing. My hope is that the ability to make explanations more natural will
  1292. cause more programmers to discover the joys of literate programming,
  1293. because I believe it's quite a pleasure to combine verbal and mathematical
  1294. skills; but perhaps I'm hoping for too much. The fact that at least one
  1295. paper has been written that is a syntactically correct {\mc ALGOL 68}
  1296. program\ref\ft{C. H. Lindsey, ALGOL 68 with fewer tears. {\sl The
  1297. Computer Journal\/ \bf15}, 176--188 (1972).} encourages me to persevere
  1298. in my hopes for the future. Perhaps we will even one day find Pulitzer
  1299. prizes awarded to computer programs.
  1300. And what about the future of \WEB? If the next year or so of trial use
  1301. shows that a lot of other people besides myself become ``hooked'' on this
  1302. method of programming, there will be many ways to incorporate the \WEB\
  1303. philosophy into a really effective programming environment. For example,
  1304. it will be worthwhile to produce a unified system that does both
  1305. tangling and compiling, instead of using separate programs as in Figure~1;
  1306. and it will also be worthwhile to carry the unification one step further,
  1307. so that run-time debugging as well as syntactic debugging can be done
  1308. entirely in terms of the \WEB\ source language. Furthermore, a \WEB-like
  1309. system could be designed to incorporate additional modularization,
  1310. so that it would be easier to compile different parts of a program
  1311. independently. The new generation of graphic workstations makes it
  1312. desirable to display selected program sections on demand, by using \TeX\
  1313. only on the sections that are of current interest, instead of producing
  1314. hardcopy for an entire document. And so on; a considerable amount of
  1315. additional research and development will be appropriate if the idea
  1316. of literate programming catches on.
  1317. \bigskip\leftline{\bf Acknowledgements}
  1318. \smallskip
  1319. {\eightrm\baselineskip9pt
  1320. \noindent The preparation of this paper was supported in part by
  1321. the National Science Foundation under grants IST-8201926 and MCS-8300984,
  1322. and by the System Development Foundation. `\TeX' is a trademark of the
  1323. American Mathematical Society.\par}
  1324. \enddoublecolumns % prepare for the references
  1325. \bigskip\bigskip
  1326. \hbox to\pagewidth{\hss\bf REFERENCES\hss\strut}
  1327. \CJrule width\pagewidth
  1328. \bigskip
  1329. \begindoublecolumns
  1330. \let\rm=\eightss \let\sl=\eightssi \let\bf=\eightssb \rm
  1331. \baselineskip=9pt
  1332. \tolerance=1000
  1333. \references
  1334. \bigskip
  1335. \noindent
  1336. Received September 1983
  1337. \enddoublecolumns
  1338. \kern6mm
  1339. \CJrule width\pagewidth
  1340.